from django.db import models

from django.utils import timezone

from MirrorCore.models.User import User
from MirrorCore.models.Collection import Collection
from MirrorCore.models.Content import Article, Essay

"""
    基本badge和其变体
"""


class Badge(models.Model):
    """
    成就系统基本量化单元，
    creator 徽章标记的作者 避免循环引入需要再获得一层
    name:   名称
    brief:  简要说明
    photo:  相应图片
    """
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50, blank=False,unique=True)
    brief = models.CharField(max_length=1000, blank=True, null=True)
    photo = models.URLField(null=True, blank=True)
    color = models.CharField(max_length=20,default="#2ed573")
    icon = models.CharField(max_length=20,default="bookmarkSharp")
    type = models.CharField(max_length=10,blank=True)     # naiveui 默认的配色方案
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return str(self.name)


class AuthorBadge(models.Model):
    """
    背包储存
    badge  包内徽章种类
    count   数量
    setting 相关呈现设置
    """
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                              related_name='badges',
                              related_query_name='badge'
                              )
    badge = models.ForeignKey(Badge, on_delete=models.CASCADE)
    count = models.BigIntegerField(default=0)

    def __str__(self):
        return '{}_{}'.format(str(self.badge), str(self.count))


class EssayBadge(models.Model):
    """ 同 essay
    一个tag只代表一个
    badge   徽章
    count   徽章数量

    create_time 创建日期
    """
    essay = models.ForeignKey(Essay, on_delete=models.CASCADE,
                              related_name='badges',
                              related_query_name='badge'
                              )
    badge = models.ForeignKey(Badge, on_delete=models.CASCADE,related_name='essaybadges')

    count = models.BigIntegerField(default=0)

    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '{}_{}'.format(str(self.badge), str(self.count))


class ArticleBadge(models.Model):
    """ 同 Article
    一个tag只代表一个
    article
    badge   徽章
    count   徽章数量

    create_time 创建日期
    """
    article = models.ForeignKey(Article, on_delete=models.CASCADE, null=False,
                                related_name='badges',
                                related_query_name='badge')

    badge = models.ForeignKey(Badge, on_delete=models.CASCADE)

    count = models.BigIntegerField(default=0)

    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '{}_{}'.format(str(self.badge), str(self.count))


class CollectionBadge(models.Model):
    """ 同 essay
    一个tag只代表一个
    badge   徽章
    count   徽章数量

    create_time 创建日期
    """
    collection = models.ForeignKey(Collection, on_delete=models.CASCADE,
                                   related_name='badges',
                                   related_query_name='badge'
                                   )

    badge = models.ForeignKey(Badge, on_delete=models.CASCADE)

    count = models.BigIntegerField(default=0)

    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '{}_{}'.format(str(self.badge), str(self.count))
